home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / Bitmap Libraries 2.0 / Lisp Interface / BitMapExamples.lisp next >
Lisp/Scheme  |  1996-03-10  |  13KB  |  412 lines

  1. ;; File BitMapExamples.lisp
  2. ;;
  3. ;;  Copyright (C) 1994, 1996 by John Montbriand.  All Rights Reserved.
  4. ;;
  5. ;;  Distribute freely in areas where the laws of copyright apply.
  6. ;;
  7. ;;  Use at your own risk.
  8. ;;
  9. ;;  Do not distribute modified copies.
  10. ;;
  11. ;;  These various BitMap libraries are for free!
  12. ;;
  13. ;;  See the file BitMap.txt for details.
  14. ;;
  15. ;; Macintosh Common Lisp Foreign Function Interfaces to the BitMap Libraries
  16.  
  17. ;; BEFORE EVALUATING THIS FILE...
  18. ;; [step one]  make sure the files BitMaps.lisp and BitMapsLib.o are in
  19. ;; the Library folder in the MCL directory.  Once those files are in place,
  20. ;; the BitMaps package can be conveniently used in any program you make.  
  21.  
  22. ;; ALSO...
  23. ;; Some of the examples herein use the following fonts:
  24. ;;   Geneva, New York, Chicago
  25.  
  26.  
  27. ;; [step two] load and import the package so it can be used here
  28. ;; note: normally, these two commands would be included in any
  29. ;; program that uses the bitmaps package.
  30.  
  31. (require :bitmaps)
  32. (use-package :bitmaps)
  33.  
  34.  
  35. ;; [step three] define a few utility routines used in the examples that follow...
  36.  
  37. ;; simple-window is used in the following to make windows for demonstration
  38. (defun simple-window (width height)
  39.    "for making simple little windows for showing stuff on the screen."
  40.    (make-instance 'window
  41.        :view-position #@(10 50)
  42.        :view-size (make-point width height)
  43.        :window-title "BitMap"
  44.        :window-type :single-edge-box
  45.        :erase-anonymous-invalidations nil))
  46.  
  47. ;; dragonr adapted to lisp from the pascal found in Matthew Zeidenberg's article
  48. ;; "Snowflakes and Dragons" appearing in the August 1985 issue of MacWorld (p. 127).
  49. (defun dragonr (x1 y1 x2 y2 x3 y3 n)
  50.    "recursive dragon drawing routine"
  51.    (if (<= n 1)
  52.       (progn
  53.          (#_MoveTo x1 y1)
  54.          (#_LineTo x2 y2)
  55.          (#_LineTo x3 y3))
  56.       (let* ((x4 (truncate (/ (+ x1 x3) 2)))
  57.                 (y4 (truncate (/ (+ y1 y3) 2)))
  58.                 (x5 (+ x3 (- x2 x4)))
  59.                 (y5 (+ y3 (- y2 y4))))
  60.          (dragonr x2 y2 x4 y4 x1 y1 (1- n))
  61.          (dragonr x2 y2 x5 y5 x3 y3 (1- n)))))
  62.  
  63. (defun dragon-fractal (h v size n)
  64.    (dragonr (+ h size) v h (- v size) (- h size) v n))
  65.  
  66.  
  67.  
  68.  
  69. ;; [example one]  the same drawing commands done directly
  70. ;; to the screen.  here, you can see how things get drawn.  Also,
  71. ;; some of the bitmap routines are used to draw strings sideways
  72. ;; and in different positions.
  73.  
  74. (prog (wind title-bits rtitle-bits mcl-title bits-title h)
  75.    
  76.    ;; make a new window
  77.    (setf wind (simple-window 200 200))
  78.    
  79.    ;; draw into the window
  80.    (with-focused-view wind
  81.        
  82.        ;; draw the dragon fractal image
  83.        (dragon-fractal 125 110 50 11)
  84.        
  85.        ;; plot the dragon title
  86.        (setf title-bits (string-to-bitmap "Dragon" '("Chicago" 24)))
  87.        (setf h (truncate (/ (- 200 (get-bitmap-width title-bits)) 2)))
  88.        (plot-bitmap title-bits h 0 #$srcOr)
  89.        
  90.        ;; plot the MCL III title
  91.        (setf mcl-title (string-to-bitmap "MCL III" '("New York" 36)))
  92.        (setf rtitle-bits (rotate-bitmap-right mcl-title))
  93.        (plot-bitmap rtitle-bits 0 0 #$srcOr)
  94.        
  95.        
  96.        ;; plot the BitMaps title
  97.        (setf bits-title (string-to-bitmap "BitMaps" '("Geneva" 36)))
  98.        (setf h (truncate (/ (- 200 (get-bitmap-width bits-title)) 2)))
  99.        (plot-bitmap bits-title h 150 #$srcOr)
  100.        
  101.        ;; recover the bitmap storage
  102.        (kill-bitmap rtitle-bits title-bits mcl-title bits-title))
  103.    
  104.    ;; wait a few seconds
  105.    (sleep 4)
  106.    
  107.    ;; close the window
  108.    (window-close wind))
  109.  
  110.  
  111.  
  112.  
  113.  
  114. ;; [example two]  drawing into bitmaps.  Here, the same
  115. ;; drawing commands are used to create the image off-screen.
  116. ;; in the next example, this function is called before the image
  117. ;; is placed on the screen.
  118.  
  119. ;; example of how to draw to an offscreen bitmap.  note, in this
  120. ;; example we draw directly into the bitmap using the quickdraw
  121. ;; functions called in dragonr, and using the plot-bitmap routine
  122. ;; for other bitmaps created using the string-to-bitmap routine
  123. (defun make-dragon-bitmap (&optional n)
  124.    
  125.    "create a 200 by 200 bitmap containing a dragon fractal"
  126.    
  127.    (let (my-bitmap title-bits rtitle-bits mcl-title bits-title h)
  128.       
  129.       ;; create a new bitmap
  130.       (setf my-bitmap (new-bitmap 200 200))
  131.       
  132.       ;; draw into the bitmap
  133.       (with-focused-bitmap (my-bitmap)
  134.           
  135.           ;; draw the dragon fractal image
  136.           (dragon-fractal 125 110 50 (if (null n) 8 n))
  137.           
  138.           ;; plot the dragon title
  139.           (setf title-bits (string-to-bitmap "Dragon" '("Chicago" 24)))
  140.           (setf h (truncate (/ (- 200 (get-bitmap-width title-bits)) 2)))  ;; center it
  141.           (plot-bitmap title-bits h 0 #$srcOr)
  142.           
  143.           ;; plot the MCL III title
  144.           (setf mcl-title (string-to-bitmap "MCL III" '("New York" 36)))
  145.           (setf rtitle-bits (rotate-bitmap-right mcl-title))
  146.           (plot-bitmap rtitle-bits 0 5 #$srcOr)
  147.           
  148.           
  149.           ;; plot the BitMaps title
  150.           (setf bits-title (string-to-bitmap "BitMaps" '("Geneva" 36)))
  151.           (setf h (truncate (/ (- 200 (get-bitmap-width bits-title)) 2))) ;; center it
  152.           (plot-bitmap bits-title h 150 #$srcOr)
  153.           
  154.           ;; recover the bitmap storage
  155.           (kill-bitmap rtitle-bits title-bits mcl-title bits-title))
  156.       
  157.       ;; return the bitmap
  158.       my-bitmap))
  159.  
  160.  
  161.  
  162.  
  163. ;; [example three] how to put a bitmap into a window
  164.  
  165. ;; example of how to draw a bitmap in a window
  166. ;; here we draw an image on an offscreen bitmap and
  167. ;; put the result on the screen.
  168. (prog (my-bitmap wind)
  169.    
  170.    ;; make a new window
  171.    (setf wind (simple-window 200 200))
  172.    
  173.    ;; create an image in a bitmap
  174.    (setf my-bitmap (make-dragon-bitmap 11))
  175.    
  176.    ;; plot the bitmap in the window
  177.    (with-focused-view wind
  178.        (plot-bitmap my-bitmap 0 0 #$srcCopy))
  179.    
  180.    ;; recover the bitmap storage
  181.    (kill-bitmap my-bitmap)
  182.    
  183.    ;; wait a few seconds
  184.    (sleep 4)  
  185.    
  186.    ;; close the window
  187.    (window-close wind))
  188.  
  189.  
  190.  
  191.  
  192. ;; [example four] rotating a bitmap to an arbitrary angle
  193.  
  194. ;; example of how to rotate a bitmap and draw it in a window
  195. ;; here we draw an image to an offscreen bitmap, and rotate
  196. ;; the image to 36 degrees at a time.
  197. (prog (my-bitmap wind next-bitmap)
  198.    
  199.    ;; create a window for display 
  200.    (setf wind (simple-window 200 200))
  201.    
  202.    ;; create an image in a bitmap
  203.    (setf my-bitmap (make-dragon-bitmap 11))
  204.    
  205.    ;; draw into the window
  206.    (with-focused-view wind
  207.        (dotimes (i 11)
  208.           (setf next-bitmap (rotate-bitmap my-bitmap (+ i 100) 100 (* i 36)))
  209.           (plot-bitmap next-bitmap 0 0 #$srcCopy)
  210.           (kill-bitmap next-bitmap)))
  211.    
  212.    ;; recover the bitmap storage
  213.    (kill-bitmap my-bitmap)
  214.    
  215.    ;; wait a few seconds
  216.    (sleep 1)  
  217.    
  218.    ;; close the window
  219.    (window-close wind))
  220.  
  221.  
  222.  
  223. ;; [example five] logical operations on bitmaps
  224.  
  225. ;; example of how do to a logical operation on bitmaps
  226. ;; here we draw an image in a bitmap, make another one
  227. ;; containing the image flipped vertically, xor the two
  228. ;; together and put the result on the screen.
  229. (prog (my-bitmap wind other-image drawn-image)
  230.    
  231.    ;; create a window for display 
  232.    (setf wind (simple-window 200 200))
  233.    
  234.    ;; create an image in a bitmap
  235.    (setf my-bitmap (make-dragon-bitmap 11))
  236.    
  237.    ;; create another image (a frame)
  238.    (setf other-image (new-bitmap 200 200))
  239.    (with-focused-bitmap (other-image)
  240.        (#_PenSize 2 2)
  241.        (#_MoveTo 0 0)
  242.        (#_Line 198 0)
  243.        (#_Line 0 198)
  244.        (#_Line -198 0)
  245.        (#_Line 0 -198)
  246.        
  247.        (#_MoveTo 150 40)
  248.        (#_TextSize 24)
  249.        (with-pstrs ((initials "JM")) (#_DrawString initials)))
  250.    
  251.    ;; xor the two images together
  252.    (setf drawn-image (xor-bitmaps other-image my-bitmap))
  253.    
  254.    ;; plot the result in the window
  255.    (with-focused-view wind
  256.        (plot-bitmap drawn-image 0 0 #$srcCopy))
  257.    
  258.    ;; recover the bitmap storage
  259.    (kill-bitmap my-bitmap other-image drawn-image)
  260.    
  261.    ;; wait a few seconds
  262.    (sleep 4)  
  263.    
  264.    ;; close the window
  265.    (window-close wind))
  266.  
  267.  
  268.  
  269.  
  270.  
  271. ;; [example six] pixel oriented operations used for drawing
  272.  
  273. ;; example of how to set specific bits in the raster image.
  274. ;; here, we a grid (one pixel at a time) every 10 pixels
  275. ;; by inverting each pixel value, clearing intersections.
  276. ;; the entire image is transfered to the screen once after each line is drawn.
  277. (prog (my-bitmap wind)
  278.    
  279.    ;; create a window for display 
  280.    (setf wind (simple-window 200 200))
  281.    
  282.    ;; create an image in a bitmap
  283.    (setf my-bitmap (new-bitmap 200 200))
  284.    
  285.    ;; Put a letter on it
  286.    (with-focused-bitmap (my-bitmap)
  287.        (#_MoveTo 45 175)
  288.        (#_TextSize 200)
  289.        (with-pstrs ((initials "J")) (#_DrawString initials)))
  290.    
  291.    ;; draw into the window
  292.    (with-focused-view wind
  293.        (do ((x 10 (+ x 10))) ((eq x 200))
  294.           (do ((y 10 (1+ y))) ((eq y 190))
  295.              
  296.              ;; draw some dots using the pixel functions
  297.              (if (eq (rem y 10) 0)
  298.                 
  299.                 ;; clear dots where lines cross
  300.                 (clear-bitmap-pixel my-bitmap x y)
  301.                 
  302.                 ;; invert pixels in other places
  303.                 (progn
  304.                    (toggle-bitmap-pixel my-bitmap x y)
  305.                    (toggle-bitmap-pixel my-bitmap y x))))
  306.           
  307.           ;; draw to screen at the end of each line
  308.           (plot-bitmap my-bitmap 0 0 #$srcCopy)))
  309.    
  310.    ;; recover the bitmap storage
  311.    (kill-bitmap my-bitmap)
  312.    
  313.    ;; wait a few seconds
  314.    (sleep 4)  
  315.    
  316.    ;; close the window
  317.    (window-close wind))
  318.  
  319.  
  320.  
  321.  
  322. ;; [example seven] drawing in colour with bitmaps
  323.  
  324. ;; example of how to draw in different colours using bitmaps
  325. ;; here we draw successive generations of the dragon fractal
  326. ;; on the screen in different colours using colouration.
  327. (prog (my-bitmap wind colour-list) 
  328.    
  329.    ;; create a window for display 
  330.    (setf wind (simple-window 200 200))
  331.    
  332.    ;; set up a ring of colours 
  333.    (setf colour-list (list *red-color* *green-color* *blue-color* *yellow-color*))
  334.    (setf (cdr (last colour-list)) colour-list)
  335.    
  336.    ;; start drawing into the window
  337.    (with-focused-view wind
  338.        
  339.        ;; paint the window black
  340.        (set-fore-color wind *black-color*)
  341.        (#_PaintRect (pref (wptr wind) windowrecord.portrect))
  342.        
  343.        ;; loop while overlapping successive generations in different colours
  344.        (do ((i 12 (1- i)) (colour colour-list (cdr colour))) ((eq i 0))
  345.           
  346.           ;; create another image
  347.           (setf my-bitmap (make-dragon-bitmap i))
  348.           
  349.           ;; set the drawing colour
  350.           (set-fore-color wind (car colour))
  351.           
  352.           ;; add it to the image on the screen
  353.           (plot-bitmap my-bitmap 0 0 #$srcOr)
  354.           
  355.           ;; recover the bitmap storage
  356.           (kill-bitmap my-bitmap)))
  357.    
  358.    ;; wait a few seconds
  359.    (sleep 4)  
  360.    
  361.    ;; close the window
  362.    (window-close wind))
  363.  
  364.  
  365.  
  366.  
  367. ;; [example eight] drawing strings at different orientations
  368.  
  369. ;; example of how to use string to bitmap for drawing strings
  370. ;; in different orientations
  371. (prog (my-bitmap right-bitmap left-bitmap height width wind the-string index hpos)
  372.    
  373.    ;; create a window for display 
  374.    (setf wind (simple-window 200 250))
  375.    
  376.    ;; draw some rotated strings in the window
  377.    (with-focused-view wind
  378.        (dotimes (i 10)
  379.           
  380.           ;; create a bitmap containing a string
  381.           (setf index (1+ i))
  382.           (setf the-string (format nil "String ~@R (~R)" index index))
  383.           (setf my-bitmap (string-to-bitmap the-string `("Geneva" 11 :bold)))
  384.           
  385.           ;; rotate the string image right and left
  386.           (setf left-bitmap (rotate-bitmap-left my-bitmap))
  387.           (setf right-bitmap (rotate-bitmap-right my-bitmap))
  388.           
  389.           ;; calculate the horizontal position
  390.           (setf width (get-bitmap-width right-bitmap))
  391.           (setf hpos (- (* width 11) (* i width)))
  392.           
  393.           ;; put the right image at the top of the window
  394.           (plot-bitmap right-bitmap hpos 0 #$srcOr)
  395.           
  396.           ;; put the left image at the bottom of the window
  397.           (setf height (get-bitmap-height left-bitmap))
  398.           (plot-bitmap left-bitmap hpos (- 250 height) #$srcOr)
  399.           
  400.           ;; recover the bitmap storage
  401.           (kill-bitmap my-bitmap left-bitmap right-bitmap)))
  402.    
  403.    ;; wait a few seconds
  404.    (sleep 4)  
  405.    
  406.    ;; close the window
  407.    (window-close wind))
  408.  
  409.  
  410. ;; end of file BitMapExamples.lisp
  411.  
  412.